<template>
	<view class="search">
		<MusicHead title="搜索" :isShowIcon="true" iconStyle="light"></MusicHead>
		<view class="container">
			<scroll-view scroll-y="true">
				<view class="search-search">
					<text class="iconfont iconsearch"></text>
					<input 
						type="text" 
						placeholder="搜索歌曲" 
						v-model="searchWord" 
						@confirm="handleToSearch" 
						@input="handleToSuggest"
					/>
					<text 
						v-show="searchType !== 1" 
						class="iconfont iconguanbi"
						@tap="handleToClose"
					></text>
				</view>
				<block v-if="searchType === 1">
					<view class="search-history">
						<view class="search-history-head">
							<text>历史记录</text>
							<text 
								class="iconfont iconlajitong"
								@tap="handleToClear"
							></text>
						</view>
						<view class="search-history-list">
							<view 
								class="search-history-item"
								v-for="item, index in searchHistory"
								:key="index"
								@tap="handleToWord(item)"
							>
								{{ item }}
							</view>
						</view>
					</view>
					<view class="search-hot">
						<view class="search-hot-head">
							热搜榜
						</view>
						<view 
							class="search-hot-item"
							v-for="item, index in searchHot"
							:key="item.id"
							@tap="handleToWord(item.searchWord)"
						>
							<view class="search-hot-top">
								{{ index + 1 }}
							</view>
							<view class="search-hot-word">
								<view>
									{{ item.searchWord }}
									<image :src="item.iconUrl" mode=""></image>
								</view>
								<view>
									{{ item.content }}
								</view>
							</view>
							<text class="search-hot-count">{{ item.score }}</text>
						</view>
					</view>
				</block>
				<block v-else-if="searchType === 2">
					<view class="search-result">
						<view 
							class="search-result-item"
							v-for="item in searchList"
							:key="item.id"
							@tap="handleToDetail(item.id)"
						>
							<view class="search-result-word">
								<view>
									{{ item.name }}
								</view>
								<view>
									{{ item.artists[0].name }}-{{ item.album.name }}
								</view>
							</view>
							<text class="iconfont iconbofang"></text>
						</view>
					</view>
				</block>
				<block v-else-if="searchType === 3">
					<view class="search-suggest">
						<view class="search-suggest-head">
							搜索"{{ searchWord }}"
						</view>
						<view 
							class="search-suggest-item"
							v-for="item in searchSuggest"
							:key="item.keyword"
							@tap="handleToSearch(item.keyword)"
						>
							<text class="iconfont iconsearch"></text>
							{{ item.keyword }}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import MusicHead from "@/components/MusicHead.vue"
	import { searchHot, searchWord, searchSuggest } from "../../common/api"
	
	
	export default {
		data() {
			return {
				searchHot: [],
				searchWord: '',
				searchHistory: [],
				searchType: 1,
				searchList: [],
				searchSuggest: [],
			}
		},
		components: {
			MusicHead,
		},
		onLoad() {
			this.getHots()
		},
		methods: {
			handleToDetail(songId) {
				uni.navigateTo({
					url: '/pages/detail/detail?songId=' + songId
				})
			},
			handleToClose() {
				this.searchWord = ''
				this.searchType = 1
			},
			async getSearchList(word) {
				const { data } = await searchWord(word)
				if (data.code === 200) {
					this.searchList = data.result.songs
					this.searchType = 2
				}
			},
			handleToClear() {
				uni.removeStorage({
					key: 'searchHistory',
					success() {
						this.searchHistory = []
					}
				})
			},
			handleToSearch() {
				this.searchHistory.unshift(this.searchWord)
				this.searchHistory = [...new Set(this.searchHistory)]
				if (this.searchHistory.length > 10) {
					this.searchHistory.splice(0, 10)
				}
				uni.setStorage({
					key: 'searchHistory',
					data: this.searchHistory
				})
				this.getSearchList(this.searchWord)
			},
			handleToWord(word) {
				this.searchWord = word
			},
			async getHots() {
				const { data } = await searchHot()
				if (data.code === 200) {
					this.searchHot = data.data
				}
				uni.getStorage({
					key: 'searchHistory',
					success: (res) => {
						this.searchHistory = res.data
					}
				})
			},
			async handleToSuggest(ev) {
				let value = ev.detail.value
				const { data } = await searchSuggest(value)
				if (data.code === 200) {
					this.searchSuggest = data.result.allMatch
					this.searchType = 3
				}
			}
		}
	}
</script>

<style scoped>
	.search-search {
		display: flex;
		align-items: center;
		height: 160rpx;
		margin: 24rpx 30rpx 30rpx;
		height: 80rpx;
		border-radius: 50rpx;
		background-color: #f7f7f7;
	}
	.search-search text {
		font-size: 26rpx;
		margin: 0 26rpx;
	}
	.search-search input {
		font-size: 28rpx;
		flex: 1;
	}
	
	.search-history {
		padding: 0 30rpx;
	}
	
	.search-history-head {
		display: flex;
		justify-content: space-between;
		font-size: 28rpx;
	}
	
	.search-history-list {
		display: flex;
		flex-wrap: wrap;
		gap: 30rpx;
	}
	
	.search-history-item {
		margin-top: 30rpx;
		font-size: 26rpx;
		padding: 16rpx 24rpx;
		border-radius: 999rpx;
		background-color: #f7f7f7;
	}
	
	.search-hot {
		padding: 30rpx;
	}
	
	.search-hot-head {
		font-size: 28rpx;
		margin-bottom: 36rpx;
	}
	
	.search-hot-item {
		display: flex; 
		align-items: center;
		margin-bottom: 58rpx;
	}
	
	.search-hot-top {
		color: #fb2222;
		width: 60rpx;
	}
	
	.search-hot-word {
		flex: 1;
	}
	
	.search-hot-word view:nth-child(1) {
		font-size: 30rpx;
	}
	
	.search-hot-word view:nth-child(2) {
		font-size: 24rpx;
		color: #878787;
	}
	
	.search-hot-word image {
		width: 22rpx;
		height: 22rpx;
		margin-left: 8rpx;
	}
	
	.search-hot-count {
		color: #878787;
	}
	
	.search-result {
		border-top: 2rpx #e4e4e4 solid;
		padding: 30rpx;
	}
	
	.search-result-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 2rpx #e4e4e4 solid;
		padding: 30rpx 0;
	}
	
	.search-result-word {
		
	}
	
	.search-result-word view:nth-child(1) {
		font-size: 28rpx;
		color: #235790;
		margin-bottom: 12rpx;
	}
	
	.search-result-word view:nth-child(2) {
		font-size: 22rpx;
		color: #878787;
	}
	
	.search-result-item text {
		color: #878787;
		font-size: 50rpx;
	}
	
	.search-suggest {
		border-top: 2rpx #e4e4e4 solid;
		padding: 30rpx;
		font-size: 26rpx;
	}
	
	.search-suggest-head {
		color: #4574a5;
		margin-top: 74rpx;
	}
	
	.search-suggest-item {
		color: #5d5d5d;
		margin-top: 74rpx;
	}
	
	.search-suggest-item text {
		color: #bdbdbd;
		margin-right: 16rpx;
		position: relative;
		top: 2rpx;
	}
</style>
